package cc.mrbird.febs.member.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wuwenze.poi.ExcelKit;

import cc.mrbird.febs.common.annotation.ControllerEndpoint;
import cc.mrbird.febs.common.controller.BaseController;
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.member.entity.Member;
import cc.mrbird.febs.member.service.IMemberService;
import lombok.RequiredArgsConstructor;

/**
 * 会员表 Controller
 *
 * @author Lord
 * @date 2020-06-06 15:48:19
 */
@Validated
@Controller
@RequiredArgsConstructor
public class MemberController extends BaseController {

    private final IMemberService memberService;

    @GetMapping("member")
    @ResponseBody
    @RequiresPermissions("member:list")
    public FebsResponse getAllMembers(Member member) {
        return new FebsResponse().success().data(memberService.findMembers(member));
    }

    @GetMapping("member/list")
    @ResponseBody
    @RequiresPermissions("member:list")
    public FebsResponse memberList(QueryRequest request, Member member) {
        Map<String, Object> dataTable = getDataTable(this.memberService.findMembers(request, member));
        return new FebsResponse().success().data(dataTable);
    }

    @ControllerEndpoint(operation = "新增Member", exceptionMessage = "新增Member失败")
    @PostMapping("member")
    @ResponseBody
    @RequiresPermissions("member:add")
    public FebsResponse addMember(@Valid Member member) {
        this.memberService.createMember(member);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "删除Member", exceptionMessage = "删除Member失败")
    @GetMapping("member/delete")
    @ResponseBody
    @RequiresPermissions("member:delete")
    public FebsResponse deleteMember(Member member) {
        this.memberService.deleteMember(member);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "修改Member", exceptionMessage = "修改Member失败")
    @PostMapping("member/update")
    @ResponseBody
    @RequiresPermissions("member:update")
    public FebsResponse updateMember(Member member) {
        this.memberService.updateMember(member);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "修改Member", exceptionMessage = "导出Excel失败")
    @PostMapping("member/excel")
    @ResponseBody
    @RequiresPermissions("member:export")
    public void export(QueryRequest queryRequest, Member member, HttpServletResponse response) {
        List<Member> members = this.memberService.findMembers(queryRequest, member).getRecords();
        ExcelKit.$Export(Member.class, response).downXlsx(members, false);
    }
}
